package both.character;

import java.util.ArrayList;
import java.io.*;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.*;
import org.xml.sax.helpers.*;

/**
 * Class to retrieve character information from an XML file.
 * @author Edd, Sam
 */
public class CharacterInformation {

	private ArrayList<CharacterStore> characters;

	public static void main(String[] args) {
		new CharacterInformation();
	}

	public CharacterInformation() {
		characters = new ArrayList<CharacterStore>();

		try {

			SAXParserFactory factory = SAXParserFactory.newInstance();

			SAXParser saxParser = factory.newSAXParser();

			File file = new File("characters.xml");

			DefaultHandler charHandler = new CharacterHandler();

			saxParser.parse(file, charHandler);

		} catch (Exception e) {
			e.printStackTrace();
		}

		print();
	}

	public void print() {
		for (int i = 0; i < characters.size(); i++) {
			System.out.println(toString(i));
		}
	}
	
	public int getNumberOfCharacters() {
		return characters.size() + 1;
	}

	public String getCharName(int i) {
		if (characters.get(i) != null) {
			return characters.get(i).getCharName();
		} else {
			return "FAIL";
		}
	}

	public void setCharName(int i, String cName) {
		if (characters.get(i) != null) {
			characters.get(i).setCharName(cName);
		}
	}

	public String getPlayerName(int i) {
		if (characters.get(i) != null) {
			return characters.get(i).getPlayerName();
		} else {
			return "FAIL";
		}
	}
	
	public void setPlayerName(int i, String pName) {
		if (characters.get(i) != null) {
			characters.get(i).setPlayerName(pName);
		}
	}
	
	public String getCharClass(int i) {
		if (characters.get(i) != null) {
			return characters.get(i).getCharClass();
		} else {
			return "FAIL";
		}
	}
	
	public void setCharClass(int i, String charClass) {
		if (characters.get(i) != null) {
			characters.get(i).setCharClass(charClass);
		}
	}
	
	public int getLevel(int i) {
		if (characters.get(i) != null) {
			return characters.get(i).getLevel();
		} else {
			return 0;
		}
	}
	
	public void setLevel(int i, int level) {
		if (characters.get(i) != null) {
			characters.get(i).setLevel(level);
		}
	}
	
	public String getRace(int i) {
		if (characters.get(i) != null) {
			return characters.get(i).getRace();
		} else {
			return "FAIL";
		}
	}

	public void setRace(int i, String race) {
		if (characters.get(i) != null) {
			characters.get(i).setRace(race);
		}
	}
	
	public String getSize(int i) {
		if (characters.get(i) != null) {
			return characters.get(i).getSize();
		} else {
			return "FAIL";
		}
	}

	public void setSize(int i, String size) {
		if (characters.get(i) != null) {
			characters.get(i).setSize(size);
		}
	}
	
	public String getGender(int i) {
		if (characters.get(i) != null) {
			return characters.get(i).getGender();
		} else {
			return "FAIL";
		}
	}

	public void setGender(int i, String gender) {
		if (characters.get(i) != null) {
			characters.get(i).setSize(gender);
		}
	}
	
	public String getAlignment(int i) {
		if (characters.get(i) != null) {
			return characters.get(i).getAlignment();
		} else {
			return "FAIL";
		}
	}

	public void setAlignment(int i, String alignment) {
		if (characters.get(i) != null) {
			characters.get(i).setAlignment(alignment);
		}
	}
	
	public String getReligion(int i) {
		if (characters.get(i) != null) {
			return characters.get(i).getReligion();
		} else {
			return "FAIL";
		}
	}

	public void setReligion(int i, String religion) {
		if (characters.get(i) != null) {
			characters.get(i).setReligion(religion);
		}
	}
	
	public String getHeight(int i) {
		if (characters.get(i) != null) {
			return characters.get(i).getHeight();
		} else {
			return "FAIL";
		}
	}

	public void setHeight(int i, String height) {
		if (characters.get(i) != null) {
			characters.get(i).setHeight(height);
		}
	}
	
	public int getWeight(int i) {
		if (characters.get(i) != null) {
			return characters.get(i).getWeight();
		} else {
			return 0;
		}
	}

	public void setWeight(int i, int weight) {
		if (characters.get(i) != null) {
			characters.get(i).setWeight(weight);
		}
	}
	
	public String getLooks(int i) {
		if (characters.get(i) != null) {
			return characters.get(i).getLooks();
		} else {
			return "FAIL";
		}
	}

	public void setLooks(int i, String looks) {
		if (characters.get(i) != null) {
			characters.get(i).setLooks(looks);
		}
	}

	public String toString(int i) {
		return "";
	}

	public class CharacterHandler extends DefaultHandler {
		private CharacterStore cha;
		boolean charName = false;
		boolean playName = false;
		boolean charClass = false;
		boolean level = false;
		boolean race = false;
		boolean size = false;
		boolean gender = false;
		boolean alignment = false;
		boolean religion = false;
		boolean height = false;
		boolean weight = false;
		boolean looks = false;
		
		public void startElement(String namespaceURI, String localName,
				String qualifiedName, Attributes atts) throws SAXException {

			System.out.println("Found Start Element: " + qualifiedName);

			if (qualifiedName.equals("character")) {
				System.out.println("Character Found...");
				cha = new CharacterStore();
			}
			if (qualifiedName.equals("playername")) {
				playName = true;
			}
			if (qualifiedName.equals("charactername")) {
				charName = true;
			}
			if (qualifiedName.equals("charclass")) {
				charClass = true;
			}
			if (qualifiedName.equals("level")) {
				level = true;
			}
			if (qualifiedName.equals("race")) {
				race = true;
			}
			if (qualifiedName.equals("size")) {
				size = true;
			}
			if (qualifiedName.equals("gender")) {
				gender = true;
			}
			if (qualifiedName.equals("alignment")) {
				alignment = true;
			}
			if (qualifiedName.equals("religion")) {
				religion = true;
			}
			if (qualifiedName.equals("height")) {
				height = true;
			}
			if (qualifiedName.equals("weight")) {
				weight = true;
			}
			if (qualifiedName.equals("looks")) {
				looks = true;
			}
		}
		
		public void endElement(String namespaceURI, String localName,
				String qualifiedName) throws SAXException {

			System.out.println("Found End Element: " + qualifiedName);

			if (qualifiedName.equals("character")) {
				System.out.println("Character Added...");
				characters.add(cha);
				cha = null;
			}
			if (qualifiedName.equals("playername")) {
				playName = false;
			}
			if (qualifiedName.equals("charactername")) {
				charName = false;
			}
			if (qualifiedName.equals("charclass")) {
				charClass = false;
			}
			if (qualifiedName.equals("level")) {
				level = false;
			}
			if (qualifiedName.equals("race")) {
				race = false;
			}
			if (qualifiedName.equals("size")) {
				size = false;
			}
			if (qualifiedName.equals("gender")) {
				gender = false;
			}
			if (qualifiedName.equals("alignment")) {
				alignment = false;
			}
			if (qualifiedName.equals("religion")) {
				religion = false;
			}
			if (qualifiedName.equals("height")) {
				height = false;
			}
			if (qualifiedName.equals("weight")) {
				weight = false;
			}
			if (qualifiedName.equals("looks")) {
				looks = false;
			}
		}
		
		public void characters(char[] ch, int start, int length)
				throws SAXException {
			if (charName) {
				String cName = new String(ch);
				cName = cName.substring(start, start + length);
				System.out.println(cName);
				cha.setCharName(cName);
			}
			if (playName) {
				String pName = new String(ch);
				pName = pName.substring(start, start + length);
				System.out.println(pName);
				cha.setPlayerName(pName);
			}
			if (charClass) {
				String cClass = new String(ch);
				cClass = cClass.substring(start, start + length);
				System.out.println(cClass);
				cha.setCharClass(cClass);
			}
			if (level) {
				String cLevel = new String(ch);
				cLevel = cLevel.substring(start, start + length);
				System.out.println(cLevel);
				cha.setLevel(Integer.parseInt(cLevel));				
			}
			if (race) {
				String cRace = new String(ch);
				cRace = cRace.substring(start, start + length);
				System.out.println(cRace);
				cha.setRace(cRace);
			}
			if (size) {
				String cSize = new String(ch);
				cSize = cSize.substring(start, start + length);
				System.out.println(cSize);
				cha.setSize(cSize);
			}
			if (gender) {
				String cGender = new String(ch);
				cGender = cGender.substring(start, start + length);
				System.out.println(cGender);
				cha.setGender(cGender);
			}
			if (alignment) {
				String cAlignment = new String(ch);
				cAlignment = cAlignment.substring(start, start + length);
				System.out.println(cAlignment);
				cha.setAlignment(cAlignment);
			}
			if (religion) {
				String cReligion = new String(ch);
				cReligion = cReligion.substring(start, start + length);
				System.out.println(cReligion);
				cha.setReligion(cReligion);
			}
			if (height) {
				String cHeight = new String(ch);
				cHeight = cHeight.substring(start, start + length);
				System.out.println(cHeight);
				cha.setHeight(cHeight);
			}
			if (weight) {
				String cWeight = new String(ch);
				cWeight = cWeight.substring(start, start + length);
				System.out.println(cWeight);
				cha.setWeight(Integer.parseInt(cWeight));				
			}
			if (looks) {
				String cLooks = new String(ch);
				cLooks = cLooks.substring(start, start + length);
				System.out.println(cLooks);
				cha.setLooks(cLooks);
			}
		}
	}
}
